home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS / antlr / globals.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-14  |  11.0 KB  |  357 lines  |  [TEXT/MPS ]

  1. /*
  2.  * globals.c    --    File containing all variables/tables visible to all files.
  3.  *
  4.  * $Id: globals.c,v 1.8 1994/08/29 20:16:14 parrt Exp parrt $
  5.  * $Revision: 1.8 $
  6.  *
  7.  * SOFTWARE RIGHTS
  8.  *
  9.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  10.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  11.  * company may do whatever they wish with source code distributed with
  12.  * PCCTS or the code generated by PCCTS, including the incorporation of
  13.  * PCCTS, or its output, into commerical software.
  14.  * 
  15.  * We encourage users to develop software with PCCTS.  However, we do ask
  16.  * that credit is given to us for developing PCCTS.  By "credit",
  17.  * we mean that if you incorporate our source code into one of your
  18.  * programs (commercial product, research project, or otherwise) that you
  19.  * acknowledge this fact somewhere in the documentation, research report,
  20.  * etc...  If you like PCCTS and have developed a nice tool with the
  21.  * output, please mention that you developed it using PCCTS.  In
  22.  * addition, we ask that this header remain intact in our source code.
  23.  * As long as these guidelines are kept, we expect to continue enhancing
  24.  * this system and expect to make other tools available as they are
  25.  * completed.
  26.  *
  27.  * ANTLR 1.23
  28.  * Terence Parr
  29.  * Parr Research Corporation
  30.  * with Purdue University and AHPCRC, University of Minnesota
  31.  * 1989-1994
  32.  */
  33. #include <stdio.h>
  34. #ifdef __cplusplus
  35. #ifndef __STDC__
  36. #define __STDC__
  37. #endif
  38. #endif
  39. #include "set.h"
  40. #include "syn.h"
  41. #include "hash.h"
  42. #include "generic.h"
  43.  
  44. char Version[] = "1.23";    /* PCCTS version number */
  45. char VersionDef[] = "123";    /* same (except int equiv for preproc symbol) */
  46. char LexStartSymbol[] = "START";/* Name of starting lexical class/automaton */
  47.  
  48. char *RemapFileName = "remap.h";
  49. char *DlgFileName = "parser.dlg";
  50. char *DefFileName = "tokens.h";
  51. char *ErrFileName = "err.c";
  52. char *ModeFileName = "mode.h";
  53.  
  54. char *ParserName = DefaultParserName;
  55.  
  56. /* list of PCCTS supplied support symbols; these are renamed when more than
  57.  * one ANTLR-generated parsers are linked together to avoid name conflicts.
  58.  * Can't use '##' ANSIC preprocessor concat operator with K&R and:
  59.  *        #define zzskip    zzparser ## skip
  60.  * will not work for ANSI/C++ as 'zzparserskip' is created w/o zzparser
  61.  * being substituted--ack!!!
  62.  */
  63. char *StandardSymbols[] = {
  64. /* ANTLR stuff */
  65.     "zzStackOvfMsg",
  66.     "zzasp",
  67.     "zzaStack",
  68.     "inf_tokens",
  69.     "inf_text",
  70.     "inf_text_buffer",
  71.     "inf_text_buffer_ptr",
  72.     "inf_text_buffer_size",
  73.      "inf_labase",
  74.     "inf_last",
  75.     "inf_lap",
  76.     "zztokenLA",
  77.     "zztextLA",
  78.     "zzlap",
  79.     "zzlabase",
  80.     "zztoktext",
  81.     "zztoken",
  82.     "zzdirty",
  83.     "zzguessing",
  84.     "zzguess_start",
  85.     "zzresynch",
  86.     "zzinf_tokens",
  87.     "zzinf_text",
  88.     "zzinf_text_buffer",
  89.     "zzinf_labase",
  90.     "zzinf_last",
  91.     "zzfill_inf_look",
  92.     "zzFAIL",
  93.     "zzsave_antlr_state",
  94.     "zzrestore_antlr_state",
  95.     "zzsyn",
  96.     "zzset_el",
  97.     "zzset_deg",
  98.     "zzedecode",
  99.     "_zzsetmatch",
  100.     "_zzmatch",
  101.     "_inf_zzgettok",
  102. /* DLG stuff */
  103.     "zzreal_line",
  104.     "zzcharfull",
  105.     "zzerr",
  106.     "zzlextext",
  107.     "zzbegexpr",
  108.     "zzendexpr",
  109.     "zzbufsize",
  110.     "zzbegcol",
  111.     "zzendcol",
  112.     "zzline",
  113.     "zzchar",
  114.     "zzbufovf",
  115.     "zzrdstream",
  116.     "zzrdfunc",
  117.     "zzrdstr",
  118.     "zzclose_stream",
  119.     "zzsave_dlg_state",
  120.     "zzrestore_dlg_state",
  121.     "zzmode",
  122.     "zzskip",
  123.     "zzmore",
  124.     "zzreplchar",
  125.     "zzreplstr",
  126.     "zzgettok",
  127.     "zzadvance",
  128.     "zzerrstd",
  129.     "zzerr_in",
  130.     "zzconstr_attr",
  131.     "zzempty_attr",
  132.     "zzerraction",
  133.     "zztokens",            /* list of token regular expressions */
  134.     "dfa",
  135.     "accepts",
  136.     "actions",
  137.     NULL        /* must be present */
  138. };
  139.  
  140. /* list of PCCTS supplied support functions; these are renamed when more than
  141.  * one ANTLR-generated parsers are linked together to avoid name conflicts.
  142.  */
  143. char *ASTSymbols[] = {
  144.     "AST",
  145.     "zzast_sp",
  146.     "zzastStack",
  147.     "zzlink",
  148.     "zzastnew",
  149.     "zzsubchild",
  150.     "zzsubroot",
  151.     "zzpre_ast",
  152.     "zzfree_ast",
  153.     "zztmake",
  154.     "zzdup_ast",
  155.     "zztfree",
  156.     "zzdouble_link",
  157.     NULL        /* must be present */
  158. };
  159.  
  160. /* Current ambiguity examination information */
  161. int CurAmbigAlt1, CurAmbigAlt2, CurAmbigline, CurAmbigfile;
  162. char *CurAmbigbtype;
  163.  
  164.  
  165.                         /* M e t h o d  T a b l e s */
  166. /*
  167.  * The following tables are used to fill syntax diagram nodes with the correct
  168.  * function pointers for computing FIRST sets and printing themselves.
  169.  */
  170.  
  171. /* fpTraverse[node type] == pointer to function that calculates trees
  172.  * representing the FIRST sets for that node (maintains spatial info).
  173.  * We use 'struct _tree' not 'tree' due to a g++ 2.4.3 bug.
  174.  */
  175. #ifdef __cplusplus
  176. struct _tree *(*fpTraverse[NumNodeTypes+1])(... /* Node *, int, set * */) = {
  177.     NULL,
  178.     (struct _tree *(*)(...)) tJunc,
  179.     (struct _tree *(*)(...)) tRuleRef,
  180.     (struct _tree *(*)(...)) tToken,
  181.     (struct _tree *(*)(...)) tAction
  182. };
  183. #else
  184. Tree *(*fpTraverse[NumNodeTypes+1])() = {
  185.     NULL,
  186.     tJunc,
  187.     tRuleRef,
  188.     tToken,
  189.     tAction
  190. };
  191. #endif
  192.  
  193. /* fpReach[node type] == pointer to function that calculates FIRST set for
  194.  * that node. (r stands for reach).  We use 'struct _set' not 'set'
  195.  * due to a g++ 2.4.3 bug.
  196.  */
  197. #ifdef __cplusplus
  198. struct _set (*fpReach[NumNodeTypes+1])(... /* Node *, int, set * */) = {
  199.     NULL,
  200.     (struct _set (*)(...)) rJunc,
  201.     (struct _set (*)(...)) rRuleRef,
  202.     (struct _set (*)(...)) rToken,
  203.     (struct _set (*)(...)) rAction
  204. };
  205. #else
  206. set (*fpReach[NumNodeTypes+1])() = {
  207.     NULL,
  208.     rJunc,
  209.     rRuleRef,
  210.     rToken,
  211.     rAction
  212. };
  213. #endif
  214.  
  215. /* fpPrint[node type] == pointer to function that knows how to print that node. */
  216. #ifdef __cplusplus
  217. void (*fpPrint[NumNodeTypes+1])(... /* Node * */) = {
  218.     NULL,
  219.     (void (*)(...)) pJunc,
  220.     (void (*)(...)) pRuleRef,
  221.     (void (*)(...)) pToken,
  222.     (void (*)(...)) pAction
  223. };
  224. #else
  225. void (*fpPrint[NumNodeTypes+1])() = {
  226.     NULL,
  227.     pJunc,
  228.     pRuleRef,
  229.     pToken,
  230.     pAction
  231. };
  232. #endif
  233.  
  234. char *decodeJType[] = {
  235.     "invalid",
  236.     "aSubBlk",
  237.     "aOptBlk",
  238.     "aLoopBlk",
  239.     "EndBlk",
  240.     "RuleBlk",
  241.     "Generic",
  242.     "EndRule",
  243.     "aPlusBlk",
  244.     "aLoopBegin"
  245. };
  246.  
  247.  
  248.                             /* H a s h  T a b l e s */
  249.  
  250. Entry    **Tname,            /* Table of all token names (maps name to tok num)*/
  251.         **Texpr,            /* Table of all token expressions
  252.                                (maps expr to tok num) */
  253.         **Rname,            /* Table of all Rules (has ptr to start of rule) */
  254.         **Fcache,            /* Cache of First/Follow Computations */
  255.         **Tcache;            /* Tree cache; First/Follow for permute trees */
  256.  
  257.  
  258.                             /* V a r i a b l e s */
  259.  
  260. int        EpToken=0;            /* Imaginary Epsilon token number */
  261. int        CurFile= -1;        /* Index into FileStr table */
  262. char    *CurRule=NULL;        /* Pointer to current rule name */
  263. RuleEntry *CurRuleNode=NULL;/* Pointer to current rule node in syntax tree */
  264. char    *CurRetDef=NULL;    /* Pointer to current return type definition */
  265. char    *CurParmDef=NULL;    /* Pointer to current parameter definition */
  266. int        NumRules=0;            /* Rules are from 1 to n */
  267. FILE    *output=NULL;        /* current parser output file */
  268. FILE    *input=NULL;        /* current grammar input file */
  269. char    *FileStr[MaxNumFiles];/* Ptr to array of file names on command-line */
  270. int        NumFiles=0;            /* current grammar file number */
  271. #ifdef __cplusplus
  272. void    (**fpTrans)(...),    /* array of ptrs to funcs that translate nodes */
  273.          (**fpJTrans)(...);    /*  ... that translate junctions */
  274. #else
  275. void    (**fpTrans)(),        /* array of ptrs to funcs that translate nodes */
  276.          (**fpJTrans)();        /*  ... that translate junctions */
  277. #endif
  278. int        **FoStack;            /* Array of LL_k ptrs to stacks of rule numbers */
  279. int        **FoTOS;            /* FOLLOW stack top-of-stack pointers */
  280. Junction *SynDiag = NULL;    /* Pointer to start of syntax diagram */
  281. int        BlkLevel=1;            /* Current block level.  Set by antlr.g, used by
  282.                              * scanner to translate $i.j attributes */
  283. set        reserved_positions;    /* set of token positions reserved by '#token T=i' cmds */
  284. set        all_tokens;            /* set of all token types */
  285. set        imag_tokens;        /* set of all imaginary token types (EpToken, errclasses...) */
  286. ListNode *ForcedTokens = 0;    /* list of token_id/token_num pairs to remap */
  287. ListNode *MetaTokenNodes=NULL; /* list of meta token refs such as token classes etc... */
  288. int        *TokenInd=NULL;        /* an indirection level between token num and position
  289.                              * of that token def in TokenStr and ExprStr */
  290. int        LastTokenCounted=0;    /* ==TokenNum if no token renumbering (same as old TokenNum) */
  291. int        TokenNum=TokenStart;
  292. char    **TokenStr=NULL;    /* map token # to token name */
  293. char    **ExprStr=NULL;        /* map token # to expr */
  294. Junction **RulePtr=NULL;    /* map rule # to RuleBlk node of rule */
  295. ListNode *ExprOrder=NULL;    /* list of exprs as they are found in grammar */
  296. ListNode *BeforeActions=NULL;/* list of grammar actions before rules */
  297. ListNode *AfterActions=NULL;/* list of grammar actions after rules */
  298. ListNode *LexActions=NULL;    /* list of lexical actions */
  299. ListNode **Cycles=NULL;        /* list of cycles (for each k) found when
  300.                                doing FOLLOWs */
  301. ListNode *eclasses=NULL;    /* list of error classes */
  302. ListNode *tclasses=NULL;    /* list of token classes */
  303. LClass     lclass[MaxLexClasses]; /* array of lex class definitions */
  304. int         CurrentLexClass;    /* index into lclass */
  305. int         NumLexClasses=0;    /* in range 1..MaxLexClasses (init 0) */
  306.  
  307. char    *HdrAction=NULL;    /* action defined with #header */
  308. FILE    *ErrFile;            /* sets and error recovery stuff */
  309. FILE    *DefFile=NULL;        /* list of tokens, return value structs, setwd defs */
  310. int        CannotContinue=FALSE;
  311. int        OutputLL_k = 1;        /* LL_k for parsing must be power of 2 */
  312. int        action_file;        /* used to track start of action */
  313. int        action_line;
  314. int        FoundGuessBlk=0;    /* there is a (...)? block somewhere in grammar */
  315. int        pLevel=0;            /* print Level */
  316. int        pAlt1,pAlt2;        /* print "==>" in front of these alts */
  317.  
  318. /* C++ output stuff */
  319. FILE    *Parser_h,            /* where subclass of ANTLRParser goes */
  320.         *Parser_c;            /* where code for subclass of ANTLRParser goes */
  321. char    Parser_h_Name[MaxFileName+1] = "";
  322. char    Parser_c_Name[MaxFileName+1] = "";
  323. ListNode *class_actions=NULL;/* list of actions inside the #class {...} defs */
  324. char    CurrentClassName[MaxRuleName]="";
  325. int        no_classes_found=1;
  326. char    *UserTokenDefsFile;
  327. int        UserDefdTokens=0;    /* found #tokdefs? */
  328. char    *OutputDirectory=TopDirectory;
  329.  
  330.  
  331.                     /* C m d - L i n e  O p t i o n s */
  332.  
  333. int        LL_k=1;                /* how many tokens of full lookahead */
  334. int        CLL_k= -1;            /* how many tokens of compressed lookahead */
  335. int        PrintOut = FALSE;    /* print out the grammar */
  336. int        PrintAnnotate = FALSE;/* annotate printout with FIRST sets */
  337. int        CodeGen=TRUE;        /* Generate output code? */
  338. int        LexGen=TRUE;        /* Generate lexical files? (tokens.h, parser.dlg) */
  339. int        GenAST=FALSE;        /* Generate AST's? */
  340. int        GenANSI=FALSE;        /* Generate ANSI code where necessary */
  341. int        GenExprSets=TRUE;    /* use sets not (LA(1)==tok) expression lists */
  342. int        GenCR=FALSE;        /* Generate cross reference? */
  343. int        GenLineInfo=FALSE;    /* Generate # line "file" stuff? */
  344. int        TraceGen=FALSE;        /* Generate code to trace rule invocation */
  345. int        elevel=1;            /* error level for ambiguity messages */
  346. int        GenEClasseForRules=0;/* don't generate eclass for each rule */
  347. int        TreeResourceLimit= -1;/* don't limit tree resource */
  348. int        DemandLookahead = 0;/* demand/delayed lookahead or not */
  349. char    *RulePrefix = "";    /* prefix each generated rule with this */
  350. char    *stdpccts = "stdpccts.h";/* where to generate std pccts include file */
  351. int        GenStdPccts = 0;    /* don't gen stdpccts.h? */
  352. int        ParseWithPredicates = 1;
  353. int        WarningLevel = 1;
  354. int        HoistPredicateContext = 0;
  355. int        GenCC = 0;            /* Generate C++ output */
  356. int        DontCopyTokens = 1;    /* in C++ don't copy ANTLRToken passed to ANTLR */
  357.